********************************************************************
*Table A16: Robots and college enrollment at state level Enrollment*
********************************************************************

*************************************************
* 		Columns 1-3: outcome at CZ level 		*
*************************************************

*Education
use "$final_data_outcomes/czone_Students.dta", clear
merge 1:1 czone year using "$final_data_outcomes/IPEDS_EnrollmentCZ.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_outcomes/czone_StudentsDemographics.dta", assert(1 3) nogenerate

*Automation
merge 1:1 czone year using "$final_data_automation/czones_ExposureAutomation.dta", assert(1 3) nogenerate
merge 1:1 czone year using "$final_data_automation/states_ExposureAutomation.dta", assert(1 3) nogenerate

*Imports merge
merge 1:1 czone year using "$final_data_imports/czone_ExposureChina", assert(1 3) nogenerate

*Covariates
merge m:1 czone using "$final_data_covariates/czone1990_Covariates.dta", assert(3) nogenerate //beginning of period
merge m:1 czone using "$final_data_covariates/IPEDS_institutionControlsCZ.dta", keep(1 3) nogenerate

*IPEDS
*merge 1:1 statefip year using "${final_data}IPEDS_EnrollmentState.dta", keep(3) nogenerate

xtset czone year

*only first two periods
keep if year<=2000

*Standardization
foreach var in expof_us_adj expof_euro7_qo {
qui su `var'
replace `var' = (`var'-`r(mean)')/`r(sd)'
}

foreach var in public communityCollege uniTop20 uniTop30Dummy uniTop30 largest150 forProfit nonProfit numberOfUni private {
replace `var' = 0 if mi(`var')
}

egen medianAnyAid = median(anyAid), by(statefip)
	replace anyAid = medianAnyAid if mi(anyAid)

	
*** gender and race
preserve
*Create confidence intervals

gen coef =.
gen se =.
gen ci_u =.
gen ci_d =.


*Regression coefficients
local i = 1
foreach type in m f w nw {
	
eststo: ivreghdfe d_sc_`type' (expof_state_noCZ_us_adj d_exp_us_stacked = expof_state_noCZ_euro7_qo d_exp_iv_stacked) d_sc_`type'_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

replace coef = _b[expof_state_noCZ_us_adj] in `i'
replace se = _se[expof_state_noCZ_us_adj] in `i'
replace ci_u = coef + 1.96*se in `i'
replace ci_d = coef - 1.96*se in `i'

local i = `i'+1

}

*keep if _n<=6
keep if _n<=4
keep coef* se* ci*


gen position=0 if _n==1
replace position=2  if _n==2
replace position=5  if _n==3
replace position=7  if _n==4

*Figure: robots and schooling by demographics
twoway  (bar coef position, barwidth(1.2) fcolor(navy) msymbol(diamond) msize(small)) ///
		(scatter coef position, color(black) msymbol(diamond) msize(small)) ///
		(rcap ci_u ci_d position, lcolor(black) lwidth(vthin)) ///
		, ylabel(-.6(.2)1, angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(2.5) xsize(4) ytitle("State exposure excl. CZ" "point estimate", size(medium)) ///
		xscale(noline) xscale(alt) xlabel(-1 " " 0 `" "Men" " " "[1]" "' 2 `" "Women" " " "[2]" "' 5 `" "Whites" " " "[3]" "' 7 `" "Non-whites" " " "[4]" "'  8 " ", labsize(medium) noticks) ///
		text(1.52 1 "Gender", place(c) size(medium) color(black)) ///
		text(1.52 6 "Race and ethnicity", place(c) size(medium) color(black)) ///
		text(1.50 1 "_______________________", place(c) size(medium) color(black)) ///
		text(1.50 6 "_______________________", place(c) size(medium) color(black)) ///
		text(1.29 0 "__________", place(c) size(medium) color(black)) ///
		text(1.29 2 "__________", place(c) size(medium) color(black)) ///
		text(1.29 5 "__________", place(c) size(medium) color(black)) ///
		text(1.29 7 "__________", place(c) size(medium) color(black)) ///
		graphregion(color(white) fcolor(white) margin(r+20 t+10))
		graph export "$figures_appendix/Figure - A5b.png", replace
restore

*** age
*Create confidence intervals
gen coef =.
gen se =.
gen ci_u =.
gen ci_d =.


*Regression coefficients
local i = 1
foreach age in a19_24 a25_29 a30_34 a35_39 a40_44 a45_49 a50_54 a55_59 a60_64 {

eststo: ivreghdfe d_sc_`age' (expof_state_noCZ_us_adj d_exp_us_stacked = expof_state_noCZ_euro7_qo d_exp_iv_stacked) d_sc_70_90 $occupations_1990 $demographics_1990 $industry_shares_1990 $institutions_1990 [w=ipums_pop_1990], absorb(division##year statefip) cluster(statefip)

replace coef = _b[expof_state_noCZ_us_adj] in `i'
replace se = _se[expof_state_noCZ_us_adj] in `i'
replace ci_u = coef + 1.96*se in `i'
replace ci_d = coef - 1.96*se in `i'

local i = `i'+1

}

keep if _n<=9
keep coef* se* ci*


*** narrow 

gen position=0 if _n==1
replace position=2  if _n==2
replace position=4  if _n==3
replace position=6  if _n==4
replace position=8  if _n==5
replace position=10  if _n==6
replace position=12  if _n==7
replace position=14  if _n==8
replace position=16  if _n==9

*Figure: robots and schooling by age

twoway  (bar coef position, barwidth(1.2) fcolor(navy) msymbol(diamond) msize(small)) ///
		(scatter coef position, color(black) msymbol(diamond) msize(small)) ///
		(line coef position, color(black*0.75) lwidth(vthin)) ///
		(rcap ci_u ci_d position, lcolor(black) lwidth(vthin)) ///
		, ylabel(-1.2(.4)2, angle(horiz) labsize(medium) grid gmin gmax format(%5.2f)) yline(0, lcolor(black) lwidth(vthin) lpattern(shortdash)) xtitle("")  ///
		legend(off) ysize(3.5) ytitle("State exposure excl. CZ" "point estimate", size(medium)) ///
		xscale(noline) xscale(alt) xlabel(-1 " " 0 `" "19-24" " " "[1]" "' 2 `" "25-29" " " "[2]" "' 4 `" "30-34" " " "[3]" "' 6 `" "35-39" " " "[4]" "' 8 `" "40-44" " " "[5]" "' 10 `" "45-49" " " "[6]" "' 12 `" "50-54" " " "[7]" "' 14 `" "55-59" " " "[8]" "' 16 `" "60-64" " " "[9]" "' 17 " ", labsize(medium) noticks) ///
		text(2.54 0 "______", place(c) size(medium) color(black)) ///
		text(2.54 2 "______", place(c) size(medium) color(black)) ///
		text(2.54 4 "______", place(c) size(medium) color(black)) ///
		text(2.54 6 "______", place(c) size(medium) color(black)) ///
		text(2.54 8 "______", place(c) size(medium) color(black)) ///
		text(2.54 10 "______", place(c) size(medium) color(black)) ///
		text(2.54 12 "______", place(c) size(medium) color(black)) ///
		text(2.54 14 "______", place(c) size(medium) color(black)) ///
		text(2.54 16 "______", place(c) size(medium) color(black)) ///
		graphregion(color(white) fcolor(white) margin(r+5))
		graph export "$figures_appendix/Figure - A5a.png", replace

